import java.awt.*;

import javax.swing.*;

public class BuildingsLayer extends JPanel {
	Cursor cursor;
	String[] imageFile = { "base.png", "farm.png" };
	Image[] image = new Image[imageFile.length];

	public BuildingsLayer(Cursor cursor) {
		this.cursor = cursor;
		this.setFocusable(false);

		// Initialize images from their file names
		MediaTracker media = new MediaTracker(this);
		for (int i = 0; i < imageFile.length; i++) {
			image[i] = Toolkit.getDefaultToolkit().getImage(
					"graphics\\" + imageFile[i]);
			media.addImage(image[i], 0);
			try {
				media.waitForID(0);
			} catch (Exception e) {
			}

		}
	}

	public void paint(Graphics g) {
		this.setLocation(0, 0);
		int w = 850, h = 550;
		this.setSize(w, h);

		int posx, posy;
		posx = -8 + cursor.getx();
		posy = -5 + cursor.gety();
		Tileable[][] buildings = cursor.getBuildings();

		// Populate the map with building images
		for (int i = 0; i < buildings.length; i++) {
			if (buildings[i] == null) {

			} else {
				for (int j = 0; j < buildings[i].length; j++) {

					// Draw building image
					if (buildings[i][j] == null)
						break;
					int x = (buildings[i][j].getX() - posx);
					int y = (buildings[i][j].getY() - posy);
					if ((x < 0) || (x > 16) || (y < 0) || (y > 16)) {
						// Outside of areaView bounds
					} else {
						// Draw building image

						g.drawImage(image[buildings[i][j].getStatus()[1] - 10],
								x * 50, y * 50, this);
					}
				}
			}
		}
	}
}
